<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            padding: 0;
            margin: 0;
            list-style: none;
        }

        .box {
            width: 300px;
            height: 200px;
            border: 1px solid #000;
            margin: auto;
        }

        .box>.head {
            display: flex;
            justify-content: space-between;
        }

        .box>.head>li {
            flex-grow: 1;
            text-align: center;
            cursor: pointer;
        }

        .active {
            background-color: #87ceeb;
        }





        .show {
            position: relative;
            width: 300px;
            height: 170px;

        }

        .show div {
            width: 300px;
            height: 180px;
            position: absolute;
            display: none;
            text-align: center;
            line-height: 170px;

        }

        #one {
            display: block;
            background-color: pink
        }

        #two {

            background-color: red;

        }

        #three {

            background-color: green;

        }
        .active1{
            display: block !important;
        }
    </style>
</head>

<body>
    <div class="box">
        <ul class="head">
            <li class="active">标题一</li>
            <li>标题二</li>
            <li>标题三</li>
        </ul>

        <div class="show">
            <div id="one">123</div>
            <div id="two">456</div>
            <div id="three">789</div>
        </div>
    </div>

    <script>
        let list = document.querySelectorAll(".head > li");
        let main = document.querySelector(".main");
        let show = document.querySelectorAll(".show>div");
        console.log(show);
        // console.log(list);
        let lastIndex = 0; // 设置上一步操作的下标

        //  head容器每个li绑定事件
        list.forEach(function (item, index) {
            item.onclick = function () {
                // li里面的active类名全部移除
                // list.forEach(function (item) {
                //     item.classList.remove("active");
                // });
                // 清除上一个操作的类名
                list[lastIndex].classList.remove("active");
                // 当前li操作添加类名
                item.classList.add("active");
                // 保存操作完毕之后的下标
                // 点击更换内容
                // 作业(不同的标题对应不同的容器)
                show[lastIndex].classList.remove("active1");

                 show[index].classList.add("active1");
                lastIndex = index;


            }
        });
    </script>
</body>

</html>